﻿using FirstFloor.ModernUI.Windows.Controls;
using Microsoft.Practices.Unity;
using ModernUILogViewer.Business;
using ModernUILogViewer.Content;
using ModernUILogViewer.ViewModel;
using MvvmFoundation.Wpf;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Input;

namespace ModernUILogViewer.View
{
    static class ViewCommands
    {
        public static ICommand OpenProcessingDialog { get { return new MvvmFoundation.Wpf.RelayCommand<HomeViewModel>(param => OpenProcessingDialogExecute(param)); } }
        public static ICommand CloseSelfWindow { get { return new MvvmFoundation.Wpf.RelayCommand<Window>(param => CloseSelfWindowExecute(param)); } }
        public static ICommand ShowMessageBox { get { return new MvvmFoundation.Wpf.RelayCommand<string>(param => ShowMessageBoxExecute(param)); } }
        public static ICommand CopyTextToClipboard { get { return new MvvmFoundation.Wpf.RelayCommand<Object>(CopyTextToClipboardExecute); } }
        public static ICommand RegisterMessages { get { return new MvvmFoundation.Wpf.RelayCommand(RegisterMessagesExecute); } }
        public static ICommand OpenUrl { get { return new RelayCommand<string>(OpenUrlExecute); } }
        public static ICommand OpenUri { get { return new RelayCommand<Uri>(OpenUriExecute); } }
        public static ICommand InitContainer { get { return new RelayCommand(InitContainerExecute); } }
        public static ICommand InitViewModel { get { return new RelayCommand<MainViewModel>(InitViewModelExecute); } }
        public static ICommand OpenFolderLocation { get { return new RelayCommand<string>(OpenFolderLocationExecute); } }
        public static ICommand MessageOutOfDateVersion { get { return new RelayCommand<ModernUILogViewer.Model.DefaultEventArgs>(MessageOutOfDateVersionExecute); } }
        
        static void InitViewModelExecute(MainViewModel mainVM)
        {
            string uri = (string)App.Current.FindResource("updateuri");

            mainVM.NetworkVM.AppUri = uri;         
        }

        static void OpenFolderLocationExecute(string location)
        {
            if (File.Exists(location))
                Process.Start(new ProcessStartInfo("explorer.exe", "/select, " + location));
        }

        private static void InitContainerExecute()
        {
            IUnityContainer container = UnityContainerProvider.Instance;
            container.RegisterType<IDialogService>(DialogServiceNS.DLG_OPEN_EXE_FILE,
                new InjectionFactory(cont =>
                {
                    DialogService dservice = new DialogService();
                    dservice.Filter = "Executable files (*.exe)|*.exe";

                    return dservice;
                }
                    )
                );
            container.RegisterType<IDialogService>(DialogServiceNS.DLG_OPEN_MULTIPLE_LOG_FILES,
                new InjectionFactory(cont =>
                        {
                            DialogService dservice = new DialogService();
                            dservice.Filter = "Log files (*.log)|*.log|All files (*.*)|*.*";

                            return dservice;
                        }
                    )
                );
            container.RegisterType<IDialogService>(DialogServiceNS.DLG_OPEN_SAVE_INI_FILE,
                    new InjectionFactory(cont =>
                        {
                            DialogService dservice = new DialogService();
                            dservice.Filter = "Config files (*.config)|*.config|All files (*.*)|*.*";

                            return dservice;
                        }
                    )
                );
            container.RegisterType<IDialogService>(DialogServiceNS.DLG_OPEN_LOG_FILE,
                new InjectionFactory(cont =>
                        {
                            DialogService dservice = new DialogService();

                            return dservice;
                        }
                    )
                );
        }

        private static void OpenUrlExecute(string url)
        {
            Process.Start(url);
        }

        private static void OpenUriExecute(Uri uri)
        {
            if (uri != null)
                Process.Start(new ProcessStartInfo(uri.AbsoluteUri));
        }        

        private static void MessageOutOfDateVersionExecute(ModernUILogViewer.Model.DefaultEventArgs eventArgs)
        {
            ModernUILogViewer.Business.ServiceUpdater.VersionInfo lastVersionInfo = (ModernUILogViewer.Business.ServiceUpdater.VersionInfo)eventArgs.ObjArg;

            MessageBoxResult result = ModernDialog.ShowMessage(String.Format("A new version ({0}) is available, do you want to go to the homepage?", lastVersionInfo.LatestVersion), "New Version", MessageBoxButton.YesNo);

            if (result == MessageBoxResult.Yes)
            {
                Process.Start(lastVersionInfo.LatestVersionUrl);
            }
        }

        private static void RegisterMessagesExecute()
        {
            //MessengerProvider.Instance.Register(MessengerEvents.MSG_REGEX_SETTINGS_SAVED,
            //    new Action<OptionsViewModel>(SaveRegexSettingsExecute));

            //MessengerProvider.Instance.Register(MessengerEvents.MSG_REGEX_SETTINGS_RELOADED,
            //    new Action<OptionsViewModel>(LoadRegexSettingsExecute));      

            //MessengerProvider.Instance.Register(MessengerEvents.MSG_COLOR_SETTINGS_RELOADED,
            //    () => {
            //        ColorSettings settings = (ColorSettings)App.Current.FindResource("colorsettings");
            //        ReloadSettingsExecute(settings); 
            //    });

            //MessengerProvider.Instance.Register(MessengerEvents.MSG_COLOR_SETTINGS_SAVED,
            //    () =>
            //    {
            //        ColorSettings settings = (ColorSettings)App.Current.FindResource("colorsettings");
            //        SaveSettingsExecute(settings);
            //    });
            //MessengerProvider.Instance.Register<NetworkViewModel>(MessengerEvents.MSG_NETWORK_SETTINGS_RELOADED,                
            //        LoadNetworkSettingsExecute
            //    );

            //MessengerProvider.Instance.Register<NetworkViewModel>(MessengerEvents.MSG_NETWORK_SETTINGS_SAVED,
            //        SaveNetworkSettingsExecute
            //   );
        }

        private static void CopyTextToClipboardExecute<T>(T obj)
        {
            Clipboard.SetText(obj.ToString());
        }


        private static void ShowMessageBoxExecute(string param)
        {
            string[] args = param.Split('|');

            string message = args[0];
            string title = args.Length > 1 ? args[1] : String.Empty;

            ModernDialog.ShowMessage(message, title, MessageBoxButton.OK);
        }

        private static void CloseSelfWindowExecute(Window w)
        {
            w.Close();
        }

        private static void OpenProcessingDialogExecute(HomeViewModel vm)
        {
            ModernDialog dialog = new ModernDialog
            {
                Title = "Processing",
                DataContext = vm,
                Content = new ContentProcessing()
            };

            dialog.CloseButton.Visibility = Visibility.Collapsed;
            dialog.ShowDialog();

        }
    }
}
